//
//  CoreDemo.m
//  JinduExample
//
//  Created by tyndog on 2017/1/16.
//  Copyright © 2017年 GT. All rights reserved.
//

#import "CoreDemo.h"
#import <CoreText/CoreText.h>
#define kOrangeColor ([UIColor colorWithRed:1.0f green:0.5f blue:0.4f alpha:1.0])

@implementation CoreDemo


- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect bounds = CGRectMake(0.0, 0.0, 300.0, 300.0);
    CGPathAddRect(path, NULL, bounds );
    
    //把浮点数转成百分比
    NSString *numStr = [NSString stringWithFormat:@"%f", _progress];
    NSString *baiStr = [self subString:numStr];
    NSString *newStr = [NSString stringWithFormat:@"超越%@%%学校",baiStr];
    CFStringRef textString = (__bridge CFStringRef)newStr;
    
    
    CFMutableAttributedStringRef attrString =
    CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    
    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0),
                                     textString);
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = { 1.0, 0.5, 0.4, 1.0 };
    CGColorRef red = CGColorCreate(rgbColorSpace, components);
    CGColorSpaceRelease(rgbColorSpace);
    CTFontRef font = CTFontRefCTFontCreateFromUIFont([UIFont systemFontOfSize:18.0f]);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, [newStr length]), kCTFontAttributeName, font);
    
    
    CGFloat lenght = [baiStr length] + 1;
    CTFontRef font2 = CTFontRefCTFontCreateFromUIFont([UIFont systemFontOfSize:25.0f]);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(2, lenght), kCTFontAttributeName, font2);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(2, lenght),
                                   kCTForegroundColorAttributeName, red);
  
    
    CTFramesetterRef framesetter =
    CTFramesetterCreateWithAttributedString(attrString);
    CFRelease(attrString);
    
    CTFrameRef frame;
    frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0),
                                     path,NULL
                                     );
    
    CTFrameDraw(frame, context);
    CFRelease(frame);
    CFRelease(path);
    CFRelease(framesetter);
}

- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    [self setNeedsDisplay];
}

- (NSString *)subString:(NSString *)numStr{
    
    NSInteger count = 0;
    NSInteger lenght = [numStr length];
    for (NSInteger i = lenght - 1; i >= 0; i--) {
        @autoreleasepool {
            NSRange range = NSMakeRange(i, 1);
            NSString *subStr = [numStr substringWithRange:range];
            if ([subStr isEqualToString:@"0"]) {
                count++;
            }else {
                if([subStr isEqualToString:@"."]){
                    count++;
                }
                break;
            }
        }
    }
    NSRange range = NSMakeRange(0, lenght - count);
    return [numStr substringWithRange:range];
}

static CTFontRef CTFontRefCTFontCreateFromUIFont(UIFont *font)
{
    CTFontRef ctFont =CTFontCreateWithName((CFStringRef)font.fontName,
                                           font.pointSize,
                                           NULL);
    return ctFont;
}
@end
